#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define INPUT 0
#define OUTPUT 1
int main()
{
    pid_t result1;
    pid_t result2;
    int result;
    int file_description[2];
    char *msg1 = "child1 is sending a message"; // message to send
    char *msg2 = "child2 is sending a message"; // message to send
    char buf[256]; // buffer to store the message
    int read_count;
    result=pipe(file_description);

    result1=fork();
    if(result1==0)
    {
        close(file_description[INPUT]);
        result = write(file_description[OUTPUT], msg1, strlen(msg1));
    }
    else if(result1>0)
    {
        result2=fork();
    }
    if(result2==0)
    {
        close(file_description[INPUT]);
        result = write(file_description[OUTPUT], msg2, strlen(msg2));
    }
    if(result1>0 && result2>0)
    {
        close(file_description[OUTPUT]);
        read_count = read(file_description[INPUT], buf, sizeof(buf));
        //if(read_count>0)
        //{
            buf[read_count] = '\0'; // set the end of string
            printf("%d bytes of data received from spawned process: %s\n",read_count, buf);
        //}
        close(file_description[INPUT]);
    }
    return (EXIT_SUCCESS);
    return 0;
}
